package Model
{
	import Model.Topics;
	import Model.User;
	
	import mx.collections.ArrayCollection;
	
	[Bindable]
	public class Users
	{
		public static var _instance:Users;
		private var _all:ArrayCollection;
		private var _visitor:User;
		
		public function Users()
		{
			if(_instance != null)
			{
				throw new Error("Users cannot be instanciated. You can only use it's properties");
			}
			_instance = this;
		}
		
		public static function get instance():Users
		{
			if(_instance == null) 
			{
				_instance = new Users();
			}
			return _instance;
		}
		
		public function get all():ArrayCollection
		{
			return _all;
		}
		
		public function get visitor():User
		{
			return _visitor;
		}
		
		public function set visitor(visitor:User):void
		{
			_visitor  = visitor;
		}
		
		public function set all(users:ArrayCollection):void
		{
			_all = new ArrayCollection(users.source);
		}
		
		
		public function getWritingUsers():ArrayCollection
		{
			var i:int;
			var writingUsers:ArrayCollection = new ArrayCollection();
			for(i = 0; i < _all.length; i++)
			{
				if(_all[i].WritingStatus == 1 &&  _all[i].writingInTopic == Topics.instance.current().TopicID)
				{
					writingUsers.addItem(_all[i]);	
				}
				
			}
			return writingUsers;
			
		}
		
		public function getUser(id:int):User
		{
			for(var i:int = 0; i<_all.length; i++)
			{
				if(_all[i].UserID == id)
					return _all[i];
			}
			return null;
		}
		
	}
}